package com.lxy.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lxy.common.R;
import com.lxy.entity.*;
import com.lxy.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class ScheduledTasks {
    @Autowired
    private RiderService riderService;
    @Autowired
    private HistoryOrderService historyOrderService;
    @Autowired
    private HistorySendOrderService historySendOrderService;
    @Autowired
    private BuyOrderService buyOrderService;
    @Autowired
    private SendOrderService sendOrderService;

    /**
     * 每月总结骑手评分
     */
    @Scheduled(cron = "0 0 0 1 * ? ")//每月触发一次
    public void systemDispatchesOrders(){
        List<Rider> list = riderService.list();
        for (Rider rider : list) {
            Double apprise=0D;
            int count=0;
            Long riderId= rider.getId();
            //获取帮我买订单的全部评价
            LambdaQueryWrapper<HistoryOrder> historyOrderLambdaQueryWrapper=new LambdaQueryWrapper<>();
            historyOrderLambdaQueryWrapper.eq(HistoryOrder::getRiderId,riderId);
            historyOrderLambdaQueryWrapper.orderByDesc(HistoryOrder::getCreateTime);
            List<HistoryOrder> historyOrderList = historyOrderService.list(historyOrderLambdaQueryWrapper);
            if(historyOrderList==null||historyOrderList.isEmpty()){
                //这个月没有接单
            }else {
                count= historyOrderService.count(historyOrderLambdaQueryWrapper);
                for (HistoryOrder historyOrder : historyOrderList) {
                    LocalDateTime time1=LocalDateTime.now();
                    LocalDateTime time2 = historyOrder.getCreateTime();
                    long days = Duration.between(time2, time1).toDays();
                    if(days>31){
                        break;
                    }
                    apprise+=historyOrder.getRiderAppraise();
                }

            }
            //获取帮我取、送订单的全部评价
            LambdaQueryWrapper<HistorySendOrder> historySendOrderLambdaQueryWrapper=new LambdaQueryWrapper<>();
            historySendOrderLambdaQueryWrapper.eq(HistorySendOrder::getRiderId,riderId);
            historySendOrderLambdaQueryWrapper.orderByDesc(HistorySendOrder::getCreateTime);
            List<HistorySendOrder> historySendOrderList = historySendOrderService.list(historySendOrderLambdaQueryWrapper);
            if(historySendOrderList==null||historySendOrderList.isEmpty()){
                //这个月没有接单
            }else{
                count+=historySendOrderService.count(historySendOrderLambdaQueryWrapper);
                for (HistorySendOrder historySendOrder : historySendOrderList) {
                    LocalDateTime time1=LocalDateTime.now();
                    LocalDateTime time2 = historySendOrder.getCreateTime();
                    long days = Duration.between(time2, time1).toDays();
                    if(days>31){
                        break;
                    }
                    apprise+=historySendOrder.getRiderAppraise();

                }
            }
            if(count==0){
                //本月没有接单，默认评价为3.0
                rider.setMonthAppraise(BigDecimal.valueOf(3.0));
            }else{
                double avg=apprise/count;
                rider.setMonthAppraise(BigDecimal.valueOf(avg));
            }
            riderService.updateById(rider);
        }
    }

    /**
     * 处理超时订单
     */
    @Scheduled(cron = "0 0 1 * * ?")//每天凌晨1点触发一次
    public void dealOverTimeOrders(){
        //处理帮我买订单
        LambdaQueryWrapper<BuyOrder> buyOrderLambdaQueryWrapper=new LambdaQueryWrapper<>();
        buyOrderLambdaQueryWrapper.eq(BuyOrder::getStatus,0);
        buyOrderLambdaQueryWrapper.orderByAsc(BuyOrder::getCreateTime);
        List<BuyOrder> buyOrderList = buyOrderService.list(buyOrderLambdaQueryWrapper);
        if(buyOrderList==null||buyOrderList.isEmpty()){
            //没有未处理订单
        }else{
            for (BuyOrder buyOrder : buyOrderList) {
                LocalDateTime time1=LocalDateTime.now();
                LocalDateTime time2 = buyOrder.getCreateTime();
                long days = Duration.between(time2, time1).toDays();
                if(days<1){
                    break;
                }
                buyOrderService.removeById(buyOrder);
            }
        }
        //处理帮我送、取订单
        LambdaQueryWrapper<SendOrder> sendOrderLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sendOrderLambdaQueryWrapper.eq(SendOrder::getStatus,0);
        sendOrderLambdaQueryWrapper.orderByAsc(SendOrder::getCreateTime);
        List<SendOrder> sendOrderList = sendOrderService.list(sendOrderLambdaQueryWrapper);
        if(sendOrderList==null||sendOrderList.isEmpty()){
            //没有未处理订单
        }else {
            for (SendOrder sendOrder : sendOrderList) {
                LocalDateTime time1=LocalDateTime.now();
                LocalDateTime time2 = sendOrder.getCreateTime();
                long days = Duration.between(time2, time1).toDays();
                if(days<1){
                    break;
                }
                sendOrderService.removeById(sendOrder);
            }
        }
    }















}
